package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.system.domain.vo.SelectOption;
import com.ruoyi.system.domain.vo.TransferData;
import com.ruoyi.system.domain.vo.UpdataStudentsMap;
import com.ruoyi.system.service.ISchoolStudentService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SchoolClass;
import com.ruoyi.system.service.ISchoolClassService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 班级信息Controller
 * 
 * @author ruoyi
 * @date 2025-06-29
 */
@RestController
@RequestMapping("/system/class")
public class SchoolClassController extends BaseController
{
    @Autowired
    private ISchoolClassService schoolClassService;

    /**
     * 查询班级信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:class:list')")
    @GetMapping("/list")
    public TableDataInfo list(SchoolClass schoolClass)
    {
        startPage();
        List<SchoolClass> list = schoolClassService.selectSchoolClassList(schoolClass);
        return getDataTable(list);
    }

    /**
     * 导出班级信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:class:export')")
    @Log(title = "班级信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SchoolClass schoolClass)
    {
        List<SchoolClass> list = schoolClassService.selectSchoolClassList(schoolClass);
        ExcelUtil<SchoolClass> util = new ExcelUtil<SchoolClass>(SchoolClass.class);
        util.exportExcel(response, list, "班级信息数据");
    }

    /**
     * 获取班级信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:class:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(schoolClassService.selectSchoolClassById(id));
    }

    /**
     * 新增班级信息
     */
    @PreAuthorize("@ss.hasPermi('system:class:add')")
    @Log(title = "班级信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SchoolClass schoolClass)
    {
        return toAjax(schoolClassService.insertSchoolClass(schoolClass));
    }

    /**
     * 修改班级信息
     */
    @PreAuthorize("@ss.hasPermi('system:class:edit')")
    @Log(title = "班级信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SchoolClass schoolClass)
    {
        return toAjax(schoolClassService.updateSchoolClass(schoolClass));
    }

    /**
     * 删除班级信息
     */
    @PreAuthorize("@ss.hasPermi('system:class:remove')")
    @Log(title = "班级信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(schoolClassService.deleteSchoolClassByIds(ids));
    }

    @GetMapping("/getClassSelection")
    public AjaxResult getClassSelection(){
        List<SelectOption> selectOptions = new ArrayList<>();
//        selectOptions.add(new SelectOption(1, "Java1班"));
//        selectOptions.add(new SelectOption(2, "Java2班"));
//        selectOptions.add(new SelectOption(3, "Java3班"));
//        selectOptions.add(new SelectOption(4, "Java4班"));
        //调用service层方法获取真实数据
        selectOptions = schoolClassService.getSelectOptionList();
        return success(selectOptions);
    }


    // 班级的controller注入学生的服务
    @Autowired
    private ISchoolStudentService schoolStudentService;

    @GetMapping(value = "/getMyClassStudentsIds/{id}")
    public AjaxResult getMyClassStudentsIds(@PathVariable("id") Long id)
    {
        // 后台假数据
        List<Long> ids = new ArrayList<>();
//        ids.add(1L);
//        ids.add(2L);
        ids = schoolStudentService.getStudentsIdsByClassId(id);
        return success(ids);
    }

    @GetMapping(value = "/getNoClassAndThisClassStudentsInfo/{id}")
    public AjaxResult getNoClassAndThisClassStudentsInfo(@PathVariable("id") Long id)
    {
        List<TransferData> transferDatas
                = schoolStudentService.getNoClassAndThisClassStudentsInfo(id);
        return success(transferDatas);
    }

    @PutMapping("/updateThisClassStudents")
    public AjaxResult updateThisClassStudents(@RequestBody  UpdataStudentsMap map)
//    public AjaxResult updateThisClassStudents(@RequestBody HashMap<String, Object> map)
    {
//        System.out.println(map.get("classId"));
//        System.out.println(map.get("ids"));

        schoolStudentService.updateThisClassStudents(map.getIds(), map.getClassId());
        return success("调用成功");
    }
}
